package iscript.gameFramework.GameLite.directEvent
{
	
	import flash.utils.Dictionary;
	
	
	/**
	 * 销毁信息集合
	 */
	internal class DirectEventDestroyPool
	{
		
		internal var destroyEventName:Vector.<String>;
		internal var destroyEventAction:Array;
		
		private var _listenerEventPool:DirectEventListenerPool;
		
		public function DirectEventDestroyPool(listenerEventPool:DirectEventListenerPool)
		{
			destroyEventName = new Vector.<String>();
			destroyEventAction = new Array();
			_listenerEventPool = listenerEventPool;
		}
		
		public function add(eventName:String, action:*):void
		{
			if(!hasEvent(eventName,action)){
				destroyEventName.push(eventName);
				destroyEventAction.push(action);
			}
		}
		
		public function hasEvent(eventName:String, action:*):Boolean {
			var index:int = destroyEventAction.indexOf(action)    
			if (index == -1) return false
			if (index != -1 && destroyEventName[index] != eventName ) return false;
			return true;
			}
		
		
		
		public function remove():void
		{
			var destroyLen:int = destroyEventName.length - 1;
			var currentEventName:String;
			var currentEventAction:*;
			
			for (var j:int = destroyLen; j >= 0; j--)
			{
				//以要销毁的作为标准,先移除监听的对应数据在移除要销毁的队列数据
				currentEventName = destroyEventName[j];
				currentEventAction = destroyEventAction[j];
				for (var i:int = _listenerEventPool.count-1; i >= 0; i--)
				{
					//匹配消息号和消息脚本
					if (currentEventName == _listenerEventPool.registerEventName[i] && currentEventAction == _listenerEventPool.registerEventAction[i]) {
						  _listenerEventPool.remove(currentEventName, currentEventAction);
						_removeLgoic(currentEventName, currentEventAction);
						}
				}
			}	
		}
		
		private function _removeLgoic(eventName:String, action:*):void
		{
			var index:Vector.<int> = new Vector.<int>();
			for (var i:int = 0; i < destroyEventName.length; i++)
			{
				if (destroyEventName[i] == eventName && destroyEventAction[i] == action)
				{
					index.push(i);
				}
			}
			index.reverse();
			var len:int = index.length - 1;
			var targetIndex:int;
			for (var j:int = len; j >= 0; j--)
			{
				targetIndex = index[j]
				destroyEventName.splice(targetIndex, 1);
				destroyEventAction.splice(targetIndex, 1);
			}
		}
	}

}